package com.spaceobj.chess.service;

import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson2.JSON;
import com.spaceobj.chess.common.RedisKeyConstants;
import com.spaceobj.chess.entity.GameState;
import com.spaceobj.chess.entity.MatchQueueItem;
import com.spaceobj.chess.entity.UserSession;

@Service
public class RedisService {
    @Autowired
    private StringRedisTemplate redisTemplate;

    // 游戏状态管理
    public void saveGameState(String gameId, GameState state) {
        String key = RedisKeyConstants.GAME_STATE_KEY + gameId;
        redisTemplate.opsForValue().set(key, JSON.toJSONString(state));
        redisTemplate.expire(key, 24, TimeUnit.HOURS);
    }

    public GameState getGameState(String gameId) {
        String key = RedisKeyConstants.GAME_STATE_KEY + gameId;
        String value = redisTemplate.opsForValue().get(key);
        return value != null ? JSON.parseObject(value, GameState.class) : null;
    }

    // 用户会话管理
    public void saveUserSession(Integer userId, UserSession session) {
        String key = RedisKeyConstants.USER_SESSION_KEY + userId;
        redisTemplate.opsForValue().set(key, JSON.toJSONString(session));
        redisTemplate.expire(key, 1, TimeUnit.HOURS);
    }

    public UserSession getUserSession(Integer userId) {
        String key = RedisKeyConstants.USER_SESSION_KEY + userId;
        String value = redisTemplate.opsForValue().get(key);
        return value != null ? JSON.parseObject(value, UserSession.class) : null;
    }

    // 匹配队列管理
    public void addToMatchQueue(String matchMode, MatchQueueItem item) {
        String key = RedisKeyConstants.MATCH_QUEUE_KEY + matchMode;
        redisTemplate.opsForList().rightPush(key, JSON.toJSONString(item));
    }

    public MatchQueueItem getFromMatchQueue(String matchMode) {
        String key = RedisKeyConstants.MATCH_QUEUE_KEY + matchMode;
        String value = redisTemplate.opsForList().leftPop(key);
        return value != null ? JSON.parseObject(value, MatchQueueItem.class) : null;
    }

    // 用户在线状态管理
    public void updateUserOnlineStatus(Integer userId, boolean isOnline) {
        String key = RedisKeyConstants.USER_ONLINE_KEY;
        if (isOnline) {
            redisTemplate.opsForSet().add(key, userId.toString());
        } else {
            redisTemplate.opsForSet().remove(key, userId.toString());
        }
    }

    public boolean isUserOnline(Integer userId) {
        String key = RedisKeyConstants.USER_ONLINE_KEY;
        return Boolean.TRUE.equals(redisTemplate.opsForSet().isMember(key, userId.toString()));
    }

    public Set<String> getOnlineUsers() {
        return redisTemplate.opsForSet().members(RedisKeyConstants.USER_ONLINE_KEY);
    }

    // 观战者列表管理
    public void addSpectator(String gameId, Integer spectatorId) {
        String key = RedisKeyConstants.SPECTATOR_LIST_KEY + gameId;
        redisTemplate.opsForSet().add(key, spectatorId.toString());
    }

    public void removeSpectator(String gameId, Integer spectatorId) {
        String key = RedisKeyConstants.SPECTATOR_LIST_KEY + gameId;
        redisTemplate.opsForSet().remove(key, spectatorId.toString());
    }

    public Set<String> getSpectators(String gameId) {
        String key = RedisKeyConstants.SPECTATOR_LIST_KEY + gameId;
        return redisTemplate.opsForSet().members(key);
    }
} 